﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

/// <summary>
/// CheckDataFormat 的摘要说明
/// </summary>
public class CheckDataFormat
{
    public CheckDataFormat()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }

    #region ****** Method ******
    /// <summary>
    /// 判断输入的字符串是否是数字，不包含小数点
    /// </summary>
    /// <param name="str">字符串</param>
    /// <returns></returns>
    public static bool IsNumeric(string strInput)
    {
        if (strInput == null || strInput.Length == 0)
            return false;
        System.Text.ASCIIEncoding ascii = new System.Text.ASCIIEncoding();
        byte[] byteStr = ascii.GetBytes(strInput);
        foreach (byte c in byteStr)
        {
            if (c < 48 || c > 57)
            {
                return false;
            }
        }
        return true;
    }
    /// <summary>
    /// 判断输入的字符串是否是数字，不包含小数点
    /// </summary>
    /// <param name="strInput">验证的字符串</param>
    /// <returns></returns>
    public static bool IsNumericType(string strInput)
    {

        string strIsOOk = "1234567890";
        for (int i = 0; i < strInput.Length; i++)
        {
            if (strIsOOk.IndexOf(strInput[i]) < 0)
            {
                return false;
            }
        }
        return true;

    }

    /// <summary>
    /// 判断输入的字符串是否是字母或者是数字
    /// </summary>
    /// <param name="strInput">输入的字符串</param>
    /// <returns>bool返回真的话表示成功</returns>
    public static bool isLetterAndNumeric(string strInput)
    {
        strInput = strInput.Trim();
        bool flag = true;
        if (strInput.Equals(""))
        {
            flag = false;
        }
        string strIsOk = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_-";
        for (int i = 0; i < strInput.Length; i++)
        {
            if (strIsOk.IndexOf(strInput[i]) < 0)
            {
                flag = false;
            }
        }
        return flag;
    }


    /// <summary>
    /// 判断输入的字符串是否是字母或者是数字
    /// </summary>
    /// <param name="strInput">输入的字符串</param>
    /// <returns>bool返回真的话表示成功</returns>
    public static bool isLetterAndNumeric1(string strInput)
    {
        strInput = strInput.Trim();
        bool flag = true;
        if (strInput.Equals(""))
        {
            flag = false;
        }
        string strIsOk = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_-()（）";
        for (int i = 0; i < strInput.Length; i++)
        {
            if (strIsOk.IndexOf(strInput[i]) < 0)
            {
                flag = false;
            }
        }
        return flag;
    }



    /// <summary>
    /// 判断输入的字符串是否是传真号码
    /// </summary>
    /// <param name="strInput">输入的字符串</param>
    /// <returns>bool返回真的话表示成功</returns>
    public static bool isFaxNumeric(string strInput)
    {
        strInput = strInput.Trim();
        bool flag = true;
        if (strInput.Equals(""))
        {
            flag = false;
        }
        string strIsOk = "1234567890_*-()（）";
        for (int i = 0; i < strInput.Length; i++)
        {
            if (strIsOk.IndexOf(strInput[i]) < 0)
            {
                flag = false;
            }
        }
        return flag;
    }
    /// <summary>
    /// 返回转换后的非法字符
    /// </summary>
    /// <param name="strInput">输入的非法字符</param>
    /// <returns>转换后的非法字符</returns>
    public static string GetLicetCharacter(string strInput)
    {
        strInput = strInput.Trim();
        string returnStr = "";
        //string strIsOk = "`!@#$%^&*+/*?:;><[]{}|,.~=";
        string strIsOk = "";
        for (int i = 0; i < strInput.Length; i++)
        {
            if (strInput[i].ToString().Equals("'"))
            {
                returnStr += strInput[i].ToString().Replace("'", "");
            }
            else if (strIsOk.IndexOf(strInput[i]) >= 0)
            {
                returnStr += strInput[i].ToString().Replace(strInput[i].ToString(), "[" + strInput[i].ToString() + "]"); ;
            }
            else
            {
                returnStr += strInput[i].ToString();
            }
        }
        return returnStr;
    }
    /// <summary>
    /// 返回转换后的非法字符
    /// </summary>
    /// <param name="strInput">输入的非法字符</param>
    /// <returns>转换后的非法字符</returns>
    public static string GetLicetCharacter2(string strInput)
    {
        strInput = strInput.Trim();
        string returnStr = "";
        string strIsOk = "`!@#$%^&*_+/*?:;><[]{}|,.~=";
        for (int i = 0; i < strInput.Length; i++)
        {
            if (strInput[i].ToString().Equals("'"))
            {
                returnStr += strInput[i].ToString().Replace("'", "''");
            }
            else if (strIsOk.IndexOf(strInput[i]) >= 0)
            {
                returnStr += strInput[i].ToString().Replace(strInput[i].ToString(), "[" + strInput[i].ToString() + "]"); ;
            }
            else
            {
                returnStr += strInput[i].ToString();
            }
        }
        return returnStr;
    }
    /// <summary>
    /// 是否包含特殊字符
    /// </summary>
    /// <param name="strInput"></param>
    /// <returns></returns>
    public static bool HaveLicetCharacter(string strInput)
    {
        strInput = strInput.Trim();

        string strIsOk = "'’!@#`$%^&*~/*?:;>`<[]{}`！＠＃＄％︿＆＊？＞＜～｛｝［］：／《》－、、‘";
        for (int i = 0; i < strInput.Length; i++)
        {

            if (strIsOk.IndexOf(strInput[i]) >= 0)
            {
                return false;
            }

        }
        return true;
    }
    /// <summary>
    /// 是否包含特殊字符
    /// </summary>
    /// <param name="strInput"></param>
    /// <returns></returns>
    public static bool HaveLicetCharacter1(string strInput)
    {
        strInput = strInput.Trim();

        string strIsOk = "!@#`$%^&*~*?:;>`<[]{`！＠＃＄％︿＆＊？＞＜～｛｝［］：《》－、、‘";
        for (int i = 0; i < strInput.Length; i++)
        {

            if (strIsOk.IndexOf(strInput[i]) >= 0)
            {
                return false;
            }

        }
        return true;
    }
    /// <summary>
    /// 是否包含特殊字符
    /// </summary>
    /// <param name="strInput"></param>
    /// <returns></returns>
    public static bool HaveLicetCharacterAll(string strInput)
    {
        strInput = strInput.Trim();

        string strIsOk = "~!@#$%^&*_+|/*./?><,';:][{}-";
        for (int i = 0; i < strInput.Length; i++)
        {

            if (strIsOk.IndexOf(strInput[i]) >= 0)
            {
                return false;
            }

        }
        return true;
    }
    /// <summary>
    /// 验证数字和小数点的
    /// </summary>
    /// <param name="strInput">strInput要验证的字符串</param>
    /// <returns>bool，返回真的话表示成功</returns>
    public static bool isNumericAndDecimal(string strInput)
    {

        bool flag = true;
        if (strInput.Equals(""))
        {
            flag = false;
        }

        else if (strInput.Equals("."))
        {
            flag = false;
        }

        else
        {
            string strIsOk = ".1234567890";
            string strIsOOk = "1234567890";
            for (int i = 0; i < strInput.Length; i++)
            {
                if (strIsOk.IndexOf(strInput[i]) < 0)
                {
                    flag = false;
                }
            }
            if (strInput.Split('.').Length > 2)
            {
                flag = false;
                for (int i = 0; i < strInput.Length; i++)
                {
                    if (strIsOOk.IndexOf(strInput[i]) < 0)
                    {
                        flag = false;
                    }
                }
            }

        }
        return flag;
    }
    /// <summary>
    /// 判断输入的数字是否是8位数字以及包含两位小数点
    /// </summary>
    /// <param name="strInput">输入要验证的数字</param>
    /// <returns>返回true，表示验证成功，false表示失败，数字的格式不正确</returns>
    public static bool SetNumericPointFormat(string strInput)
    {
        bool flag = true;

        if (isNumericAndDecimal(strInput))
        {
            if (strInput.IndexOf(".") >= 0)
            {
                if (strInput.LastIndexOf(".") != strInput.IndexOf("."))
                {
                    flag = false;
                }
                else
                {
                    if (strInput.Split('.')[1].Length > 2)
                    {
                        flag = false;
                    }
                    if (strInput.Split('.')[0].Length > 8)
                    {
                        flag = false;
                    }
                }
            }
            else
            {
                if (strInput.Length > 8)
                {
                    flag = false;
                }
            }
        }
        else
        {
            flag = false;
        }

        return flag;
    }
    /// <summary>
    /// 判断输入的数字是否是数字以及包含两位小数点
    /// </summary>
    /// <param name="strInput">输入要验证的数字</param>
    /// <returns>返回true，表示验证成功，false表示失败，数字的格式不正确</returns>
    public static bool SetNumericNinePointFormat(string strInput)
    {
        bool flag = true;

        if (isNumericAndDecimal(strInput))
        {

            if (strInput.IndexOf(".") > 0)
            {
                if (strInput.LastIndexOf(".") != strInput.IndexOf("."))
                {
                    flag = false;
                }
                else
                {
                    if (strInput.Split('.')[1].Length > 2)
                    {
                        flag = false;
                    }
                    if (strInput.Split('.')[0].Length > 9)
                    {
                        flag = false;
                    }
                }
            }
            else
            {

                if (strInput.Length > 9)
                {
                    flag = false;
                }

            }
        }
        else
        {
            flag = false;
        }

        return flag;
    }
    /// <summary>
    /// 检查输入的字符串是否是邮编的格式
    /// </summary>
    /// <param name="strInput">输入的字符串</param>
    /// <returns>true，false</returns>
    public static bool PostalCodeCheck(string strInput)
    {
        Regex re = new Regex(@"\d{6}");
        string strIsOOk = "1234567890";
        if (!re.IsMatch(strInput) || strInput.Length != 6)
        {
            return false;
        }


        for (int i = 0; i < strInput.Length; i++)
        {
            if (strIsOOk.IndexOf(strInput[i]) < 0)
            {
                return false;
            }
        }
        return true;

    }
    /// <summary>
    /// 判断输入的字符串的字节数
    /// </summary>
    /// <param name="strInput">输入的字符串</param>
    /// <returns>字符串的字节数</returns>
    public static int ReturnStrByteLenght(string strInput)
    {
        Encoding enCoding = System.Text.Encoding.GetEncoding("gb2312");
        Byte[] bytes = enCoding.GetBytes(strInput);
        return bytes.Length;
    }
    /// <summary>
    /// 将字符串中间的单引号替换成为\'
    /// </summary>
    /// <param name="strInput">需要替换的字符串</param>
    /// <returns>返回替换后的字符串</returns>
    public static string ReplaceSingleQuotes(string strInput)
    {
        string strReturn = strInput.Replace("'", "\'");
        strReturn = strReturn.Trim();
        return strReturn;
    }
    /// <summary>
    /// 返回输入日期中的星期几的index
    /// </summary>
    /// <param name="InputDateTime">输入日期</param>
    /// <returns>返回输入日期中的星期几的index</returns>
    public static int ReturnDayWeekIndex(DateTime InputDateTime)
    {

        int DayWeekIndex = 0;
        switch (InputDateTime.DayOfWeek.ToString())
        {
            case "Monday":
                DayWeekIndex = 0;
                break;
            case "Tuesday":
                DayWeekIndex = 1;
                break;
            case "Wednesday":
                DayWeekIndex = 2;
                break;
            case "Thursday":
                DayWeekIndex = 3;
                break;
            case "Friday":
                DayWeekIndex = 4;
                break;
            case "Saturday":
                DayWeekIndex = 5;
                break;
            case "Sunday":
                DayWeekIndex = 6;
                break;
            default:
                DayWeekIndex = 0;
                break;

        }
        return DayWeekIndex;


    }
    /// <summary>
    /// IP地址验证
    /// </summary>
    /// <param name="strInput">要验证的字符串</param>
    /// <returns>bool</returns>
    public static bool IPCheck(string strInput)
    {
        Regex re = new Regex(@"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$");
        if (!re.IsMatch(strInput))
        {
            return false;
        }
        return true;
    }
    /// <summary>
    /// 字符串的截取
    /// </summary>
    /// <param name="sInString">要截取的字符串</param>
    /// <param name="iCutLength">最大长度</param>
    /// <returns></returns>
    public static string CutStr(string sInString, int iCutLength)
    {
        if (sInString == null || sInString.Length == 0 || iCutLength <= 0)
            return "";
        int iCount = System.Text.Encoding.GetEncoding("GBK").GetByteCount(sInString);
        if (iCount > iCutLength)
        {
            int iLength = 0;
            for (int i = 0; i < sInString.Length; i++)
            {
                int iCharLength = System.Text.Encoding.GetEncoding("GBK").GetByteCount(new char[] { sInString[i] });
                iLength += iCharLength;
                if (iLength == iCutLength)
                {
                    sInString = sInString.Substring(0, i + 1);
                    break;
                }
                else if (iLength > iCutLength)
                {
                    sInString = sInString.Substring(0, i);
                    break;
                }
            }
        }
        return sInString;
    }
    /// <summary>
    /// 判断是否为负数
    /// </summary>
    /// <param name="inputStr">要验证的字符串</param>
    /// <returns>真：是负数；假：非负数</returns>
    public static bool IsNegative(string inputStr)
    {
        string[] arr = inputStr.Split(new char[] { '-' });
        if (arr.Length == 2 && isNumericAndDecimal(arr[1]))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public static void RemoveOtiOseZeros(ref string inputStr)
    {
        if (isNumericAndDecimal(inputStr))
        {
            while (inputStr.ToCharArray()[0].Equals('0') && !inputStr.ToCharArray()[1].Equals('.'))
            {
                inputStr = inputStr.Remove(0);
                RemoveOtiOseZeros(ref inputStr);
            }
        }
    }

    /// <summary>
    /// 是否是字母
    /// </summary>
    /// <param name="inputStr"></param>
    /// <returns></returns>
    public static bool IsLetter(string strInput)
    {
        strInput = strInput.Trim();
        bool flag = true;
        if (strInput.Equals(""))
        {
            flag = false;
        }
        string strIsOk = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        for (int i = 0; i < strInput.Length; i++)
        {
            if (strIsOk.IndexOf(strInput[i]) < 0)
            {
                flag = false;
            }
        }
        return flag;
    }

    /// <summary>
    /// 杀进程的函数
    /// </summary>
    /// <param name="processName"></param>
    public static void KillProcess(string processName)
    {
        System.Diagnostics.Process myproc = new System.Diagnostics.Process();
        //得到所有打开的进程
        try
        {
            foreach (System.Diagnostics.Process thisproc in System.Diagnostics.Process.GetProcessesByName(processName))
            {
                if (!thisproc.CloseMainWindow())
                {
                    thisproc.Dispose();
                    thisproc.Close();
                    thisproc.Kill();
                }
            }
        }
        catch (Exception)
        {

        }
    }

    #endregion
}
